// 引入库
const Koa = require('koa')
const serve = require('koa-static')
const { historyApiFallback } = require('koa2-connect-history-api-fallback')
const proxy = require('koa2-proxy-middleware')

// 创建服务器对象
const app = new Koa()
// 注册中间件
// 先处理不认识的页面路径全部指向根路径
app.use(historyApiFallback({
  whiteList: ['/api']
}))
// 处理数据请求, 进行转发
app.use(proxy({
  targets: {
    '/api/(.*)': {
      target: 'http://localhost:3000',
      changeOrigin: true
    }
  }
}))
// 静态服务
app.use(serve(__dirname+'/public'))
// 运行服务器监听端口
app.listen(3333, ()=>{
  console.log('服务器开启成功');
  console.log('http://localhost:3333');
})